package com.migao.appwidgete.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import com.migao.appwidgete.MainActivity;
import com.migao.appwidgete.R;

/**
 * Implementation of App Widget functionality.
 */
public class AppWidget4x1 extends AppWidgetProvider {

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.app_widget4x1);

        //点击跳转页面使用PendingIntent.getActivity，如果使用的是PendingIntent.getBroadcast的方法，通过广播形式，如果主程序被Kill掉
        //点击的时候会导致点击事件无法响应
        Intent intent4x1 = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent4x1 = PendingIntent.getActivity(context, 0, intent4x1, PendingIntent.FLAG_UPDATE_CURRENT);//没参数传递使用0，有是有UPDATE_CURRENT
        views.setOnClickPendingIntent(R.id.ll_4x1, pendingIntent4x1);

        // 先设置完，事件后在更新。获取数据显示，也要更新
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }
}

